import { fileURLToPath, URL } from 'node:url'
import AutoImport from "unplugin-auto-import/vite";
import { defineConfig , loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import qiankun from 'vite-plugin-qiankun'
import Components from 'unplugin-vue-components/vite'
import Unocss from 'unocss/vite'
import {
  presetAttributify,
  presetIcons,
  presetUno,
  transformerDirectives,
  transformerVariantGroup,
} from 'unocss'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import {name} from './package.json'
// https://vitejs.dev/config/
export default defineConfig(({mode, command, ssrBuild}) => {
  const root = process.cwd()
  const env = loadEnv(mode, root)
  return {
    build: {
      outDir: 'web-auth'
    },
    plugins: [
      vue(),
      vueJsx(),
      AutoImport({
        imports: ["vue"],
        dts: "src/auto-import.d.ts",
      }),
      qiankun(name, {
        useDevMode: true
      }),
      AutoImport({
        resolvers: [ElementPlusResolver({
          importStyle: 'sass',
        })],
        dts: 'src/components.d.ts',
      }),
      Components({
        resolvers: [ElementPlusResolver({
          importStyle: 'sass',
        })],
        dts: 'src/components.d.ts',
      }),
      Unocss({
        presets: [
          presetUno(),
          presetAttributify(),
          presetIcons({
            scale: 1.2,
            warn: true,
          }),
        ],
        transformers: [
          transformerDirectives(),
          transformerVariantGroup(),
        ]
      }),
    ],
    base: env.VITE_BASE_URL,
    server: {
      host: '0.0.0.0', //ip地址
      port: env.VITE_PORT as number, //端口号
      open: true //启动后是否自动打开浏览器
    },
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@use "@/styles/element/index.scss" as *;`,
        }
      }
    },
  }
})
